/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shutdown;

import Listeners.MainMenuListener;
import Listeners.SettingsListener;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

/**
 *
 * @author Joel
 */
public class AutoShutDown extends Object{

    private int dClkRes = 300;    // double-click speed in ms
    private long timeMouseDown = 0; // last mouse down time
    int lastX = 0, lastY = 0;  //  last x and y

    final private TrayIcon icon;
    private RedirectedFrame appFrame;

    public AutoShutDown(Image image) {
        super();
        //Image image = Toolkit.getDefaultToolkit().getImage("ico2.jpg");
        PopupMenu menu = new PopupMenu("Main");
        icon = new TrayIcon(image, "AutoShutDown", menu) {{
        setImageAutoSize(true);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON1){                                        
                    if(!mouseDown(e, e.getX(), e.getY()))
                        appFrame.setVisible(true);
                    }
                }
            public boolean mouseDown(MouseEvent e, int x, int y) {                
                long currentTime = e.getWhen();
                if ((lastX == x) && (lastY == y) &&
                        ((currentTime - timeMouseDown) < dClkRes)) {                    
                    return false;
                    } 
                else {                    
                    timeMouseDown = currentTime;
                    lastX = x;
                    lastY = y;
                    }
                return true;
                }
            });}};
        final MainMenuListener mml = MainMenuListener.getInstance();
        final SettingsListener sl = SettingsListener.getInstance();
        Menu settings = new Menu("settings"),  current = new Menu("current"),
                help = new Menu("Help") {{
        add(new MenuItem("Basics"));
        add(new MenuItem("Start Logging"));        
        addActionListener(mml);}};
        menu.add(help);
        menu.add(new MenuItem("Exit"));
        menu.addSeparator();                
        Menu[] menus = {new Menu("Shutdown"), new Menu("Shutdown & Restart"),
        new Menu("Hibernate"), new Menu("Logoff"), new Menu("Force Close Apps")};        
        for (int i = 0; i < menus.length; i++) {
            Menu menu1 = menus[i];
            MenuItem mi1 = new MenuItem("at time"), mi2 = new MenuItem("after amount time");
            mi1.setActionCommand(mi1.getLabel() + ":" + menu1.getLabel());          
            mi2.setActionCommand(mi2.getLabel() + ":" + menu1.getLabel());
            menu1.add(mi1);
            menu1.add(mi2);
            menu1.addActionListener(sl);
            settings.add(menu1);
            }
        current.add("none");                       
        menu.addActionListener(mml);
        menu.add(current);
        menu.add(settings);
        menu.addSeparator();
        menu.add("About...");
        appFrame = new RedirectedFrame(true);
        appFrame.setIconImage(image);
        RedirectedFrame.setDefaultLookAndFeelDecorated(true);
        JPanel timerComboBoxes = new JPanel();
        JButton set = new JButton("Set Time");
        JComboBox hrs = new JComboBox(new Vector<String>(){{
            for(int i = 0; i < 24; i++) {
                String val = String.valueOf(i);
                val = val.length() > 1 ? val : "0" + val;
                add(val);
                }
                }}){{
            setMaximumRowCount(3);            
            }};
        JComboBox min = new JComboBox(new Vector<String>(){{
            for(int i = 0; i < 60; i++){
                String val = String.valueOf(i);
                val = val.length() > 1 ? val : "0" + val;
                add(val);
                }
                }}){{
            setMaximumRowCount(3);
            }};
        JComboBox sec = new JComboBox(new Vector<String>(){{
            for(int i = 0; i < 60; i++){
                String val = String.valueOf(i);
                val = val.length() > 1 ? val : "0" + val;
                add(val);
                }
                }}){{
            setMaximumRowCount(3);
            }};
        timerComboBoxes.add(hrs);
        timerComboBoxes.add(min);
        timerComboBoxes.add(sec);
        timerComboBoxes.add(set);
        appFrame.add(timerComboBoxes, BorderLayout.SOUTH);        
        }
    
    public void setLogging(boolean bool) {
        appFrame.setLogFile(bool);
        if(bool)
            System.err.println("Logging: " + currentTime(SettingsListener.DATE_AND_TIME));
            }

    public TrayIcon getIcon() {
        return icon;
        }    

    public static String currentTime(String format) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(cal.getTime());
        }

    }
